﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using System.Data;
using System.Threading.Tasks;

namespace RazorSourceGenerator.ViewModel
{
    public class SchemaInfoViewModel : ViewModelBase
    {
        private Generator _generator;

        public SchemaInfoViewModel(Generator generator)
        {
            _generator = generator;
            TableSchemas = new ObservableCollection<TableSchemaViewModel>();
        }

        public ObservableCollection<TableSchemaViewModel> TableSchemas { get; set;}

        private TableSchemaViewModel _selectedTableSchema;
        public TableSchemaViewModel SelectedTableSchema
        {
            get { return _selectedTableSchema; }
            set 
            {
                if (_selectedTableSchema != value)
                {
                    _selectedTableSchema = value;
                    if (_selectedTableSchema != null && _selectedTableSchema.TableSchema == null)
                    {
                        _selectedTableSchema.Load();
                    }
                    RaisePropertyChanged("SelectedTableSchema");
                }
            }
        }

        public void Load()
        {
            var tables = _generator.GetDbSchema(allSchema: true);
            foreach (DataRow row in tables.Rows)
            {
                TableSchemas.Add(new TableSchemaViewModel(_generator, row["TABLE_NAME"].ToString()));
            }
        }
    }

    public class TableSchemaViewModel : ViewModelBase
    {
        private Generator _generator;

        public TableSchemaViewModel(Generator generator, string tableName)
        {
            _generator = generator;
            _tableName = tableName;
            _isLoading = false;
        }

        private string _tableName;
        public string TableName
        {
            get { return _tableName; }
            set 
            {
                if (_tableName != value)
                {
                    _tableName = value;
                    RaisePropertyChanged("TableName");
                }
            }
        }

        private DataTable _tableSchema;
        public DataTable TableSchema
        {
            get
            {
                return _tableSchema;
            }
            set
            {
                if (_tableSchema != value)
                {
                    _tableSchema = value;
                    RaisePropertyChanged("TableSchema");
                }
            }
        }

        private bool _isChecked;
        public bool IsChecked
        { 
            get
            {
                return _isChecked;
            }
            set
            {
                if (_isChecked != value)
                {
                    _isChecked = value;
                    RaisePropertyChanged("IsChecked");
                }
            }
        }

        private bool _isLoading;
        public bool IsLoading
        {
            get
            {
                return _isLoading;
            }
            set
            {
                if (_isLoading != value)
                {
                    _isLoading = value;
                    RaisePropertyChanged("IsLoading");
                }
            }
        }

        public void Load()
        {
            //_tableSchema = new ObservableCollection<dynamic>();
            //var result = _generator.GetColumnSchemas(_tableName);
            //foreach (var x in result)
            //    _tableSchema.Add(x);
            //RaisePropertyChanged("TableSchema");
            var task = new Task(() => {
                IsLoading = true;
                TableSchema = _generator.GetColumnSchemasBase(_tableName);
                IsLoading = false;
            });
            task.Start();
            //TableSchema = _generator.GetColumnSchemasBase(_tableName);
        }
        
    }
}
